package com.htfyun.fragment.settings;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.htfyun.Constants;
import com.htfyun.fragment.base.BaseWithPageNumberAdapter;
import com.htfyun.utils.DebugLog;
import com.htfyun.utils.DisplayUtil;
import com.htfyun.wifilibrary.SecurityModeEnum;
import com.htfyun.wifilibrary.WiFiManager;

import org.geometerplus.android.fbreader.FBReader;
import org.geometerplus.android.fbreader.covers.CoverManager;
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
import org.geometerplus.android.fbreader.util.AndroidImageSynchronizer;
import org.geometerplus.fbreader.Paths;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.book.BookEvent;
import org.geometerplus.fbreader.book.IBookCollection;
import org.geometerplus.fbreader.formats.PluginCollection;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filetypes.FileType;
import org.geometerplus.zlibrary.core.filetypes.FileTypeCollection;
import org.geometerplus.zlibrary.core.util.RationalNumber;
import org.geometerplus.zlibrary.ui.android.R;

import java.io.File;
import java.util.List;

/**
 * Created by sst on 2017/12/27.
 */

public class SettingsWifiRecyclerViewAdapter extends BaseWithPageNumberAdapter<ScanResult, SettingsWifiRecyclerViewAdapter.MyViewHolder> {

    private WiFiManager wiFiManager;
    
    public interface OnItemClickListener {
        void onItemClickListener(ScanResult scanResult);
    }

    private OnItemClickListener onItemClickListener;

    public SettingsWifiRecyclerViewAdapter(Context context) {
        super(context);
    }
    
    public SettingsWifiRecyclerViewAdapter(Context context, WiFiManager wiFiManager) {
        this(context);
                
        this.wiFiManager = wiFiManager;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyViewHolder getViewHolder(View view) {
        return new MyViewHolder(view);
    }

    @Override
    public int getLayoutId() {
        return R.layout.htfyun_settings_wifi_item;
    }

    @Override
    public void onItemClick(int position, ScanResult scanResult) {

        if (onItemClickListener != null) {
            onItemClickListener.onItemClickListener(scanResult);
        }
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        super.onBindViewHolder(holder, position);

        List<ScanResult> items = getItems();
        if (items.size() <= position) {
            return;
        }

        ScanResult result = items.get(position);
        holder.ssid.setText(result.SSID);

        if (wiFiManager.getSecurityMode(result) == SecurityModeEnum.OPEN) {
            holder.imgWifi.setImageResource(R.drawable.ic_wifi_level_list);
        } else {
            holder.imgWifi.setImageResource(R.drawable.ic_wifi_lock_level_list);
        }

        DebugLog.e("ssid = " + result.SSID + ", level = " + WifiManager.calculateSignalLevel(result.level, 4));
        holder.imgWifi.setImageLevel(WifiManager.calculateSignalLevel(result.level, 4));


        if (wiFiManager.isBSssidConnected(result.BSSID)) {
            holder.summary.setText(R.string.wifi_connected);
        } else if (wiFiManager.isSsidSaved(result.SSID)) {
            holder.summary.setText(R.string.wifi_remembered);
        } else if (wiFiManager.getSecurityMode(result) != SecurityModeEnum.OPEN) {
            holder.summary.setText(R.string.wifi_network_encrypt);
        } else {
            holder.summary.setText(R.string.wifi_network_open);
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imgWifi;
        TextView ssid;
        TextView summary;

        public MyViewHolder(View itemView) {
            super(itemView);
            imgWifi = (ImageView)itemView.findViewById(R.id.imgWifi);
            ssid = (TextView) itemView.findViewById(R.id.ssid);
            summary = (TextView) itemView.findViewById(R.id.summary);
        }
    }

}
